package com.kgdev.cweb.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.kgdev.cweb.beans.pub.impl.PlatePackingBean;
import com.kgdev.cweb.entities.pub.PlatePacking;

@FacesConverter(forClass = PlatePacking.class)
public class PlatePackingConverter implements Converter {

	private static PlatePackingBean platePackingObjBean;

	{
		try {
			InitialContext ic = new InitialContext();
			platePackingObjBean = (PlatePackingBean) ic
					.lookup("java:global/ChocolorEAR/ChocolorEJB/PlatePackingBean!com.kgdev.cweb.beans.pub.impl.PlatePackingBean");
		} catch (NamingException e) {
			e.printStackTrace();
		}
	}

	@Override		
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) {
		String platePacking = value;
		PlatePacking platePackingObj = platePackingObjBean.find(Integer.parseInt(platePacking));
		return platePackingObj;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) {
		PlatePacking platePackingObj = (PlatePacking) value;
		return platePackingObj.getPackingId().toString();
	}

}
